class EventProxy {
    constructor() {
        this._callbacks = {}
    }

    // 添加事件监听
    on = (ev, callback) => {
        // 事件为空的时候 设置空数组
        this._callbacks[ev] = this._callbacks[ev] || []
        this._callbacks[ev].push(callback)
        return this
    }

    // 移除事件监听
    removeListener = (eventname, callback) => {
        var calls = this._callbacks

        // 全部事件清除
        if (!eventname) {
            console.log('Remove all listeners')
            this._callbacks = {}
        } else {
            // 一个event的事件全部清除
            if (!callback) {
                console.log('Remove all listeners of %s', eventname)
                calls[eventname] = []
            } else {
                // 一个event的事件下的一个函数清除
                var list = calls[eventname]
                if (list) {
                    var l = list.length
                    for (var i = 0; i < l; i++) {
                        if (callback === list[i]) {
                            console.log('Remove a listener of %s', eventname)
                            list[i] = null
                        }
                    }
                }
            }
        }
        return this
    }

    // 触发事件
    emit = (eventname, data) => {
        var attr = this._callbacks[eventname]
        attr.forEach((fn) => fn && fn(data))
        return this
    }
}
